package com.ctrip.cap.common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ctrip.cap.common.exception.CapException;
import com.fasterxml.jackson.core.JsonProcessingException;

public class UrlUtil {

	private static final Logger logger = LoggerFactory.getLogger(UrlUtil.class);

	public static String encodeURL(Object obj) {
		String encoder = null;
		try {
			encoder = URLEncoder.encode(JsonUtil.write2Json(obj), "UTF-8")
					.replace("+", "%20");
		} catch (UnsupportedEncodingException | JsonProcessingException e) {
			logger.error(e.getMessage(), e);
			throw new CapException(e.getMessage());
		}

		return encoder;

	}
}
